package com.shtel.paas.monitorcenter.asset.management.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shtel.paas.monitorcenter.asset.management.entity.AlarmRange;
import com.shtel.paas.monitorcenter.asset.management.entity.RangeRuleHost;
import com.shtel.paas.monitorcenter.asset.management.entity.Rules;
import com.shtel.paas.monitorcenter.asset.management.mapper.AlarmRulesMapper;
import com.shtel.paas.monitorcenter.asset.management.service.AlarmRangeService;

@Service
public class AlarmRangeServiceImpl implements AlarmRangeService{
	
	@Autowired
	private AlarmRulesMapper alarmRulesMapper;
	

	@Override
	public List<AlarmRange> ruleRange(String ip) {
		
		List<AlarmRange> list_range= new ArrayList<AlarmRange>();
		
		
		//查询个性化配置
		List<RangeRuleHost> queryRuleHost = alarmRulesMapper.queryRuleHost();
		//查询通用配置
		List<Rules> queryRuleRange = alarmRulesMapper.queryRuleRange();
		
		for (Rules rul : queryRuleRange) {
			AlarmRange range = new AlarmRange();
			
			String min = rul.getMin();
			String max = rul.getMax();
			
			if(null != min && "".equals(min)) {
				String[] split_min = rul.getMin().split(",");
				range.setRulewarn(split_min[0]);
				if(split_min.length>1) {
					range.setRuledangerous(split_min[1]);
					
				}
			}
			
			if(null != max && "".equals(max)) {
				String[] split_max = max.split(",");
				range.setRulewarn(split_max[0]);
				if(split_max.length>1) {
					range.setRuledangerous(split_max[1]);
					
				}
			}
			
			range.setRulename(rul.getName());
			range.setRuledesc(rul.getDesc());
			
			range.setAlarm_frequency_id(rul.getAlarmFrequencyId());
			list_range.add(list_range.size(), range);
		}
		
		
		for (RangeRuleHost rangeRuleHost : queryRuleHost) {
			if(rangeRuleHost.getIp().equals(ip)){
				String[] split_min = rangeRuleHost.getMin().split(",");
				String[] split_max = rangeRuleHost.getMax().split(",");
				for (AlarmRange range : list_range) {
					if(range.getRulename().equals(rangeRuleHost.getName())){
						if(split_min.length==1){
						range.setRulewarn(split_min[0]);
						range.setRuledangerous(split_min[0]);}
						if(split_min.length==2){
						range.setRulewarn(split_min[0]);
						range.setRuledangerous(split_min[1]);
						range.setRuleserious(split_max[1]);}
					}
					
				}
			}
		}
		
		return list_range;
	}


	
	

}
